package cn.mdmm.task.aop;

import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.stereotype.Component;

import cn.modoumama.common.annotation.Task;


/**
 * @author Administrator
 */
@Aspect
@Component
public class TaskFilter {
    /**
     * 只拦验证码校验服务
     */
    @Pointcut("execution(* cn.mdmm.task.**Task.*(..))")
    public void task() {
    }

    @Around("task()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = null;
        
        Class<?> clazz = joinPoint.getTarget().getClass();
        Logger logger = LoggerFactory.getLogger(clazz);
        Signature signature = joinPoint.getSignature();    
        MethodSignature methodSignature = (MethodSignature)signature;    
        Method targetMethod = methodSignature.getMethod();

        Task task = AnnotationUtils.findAnnotation(targetMethod, Task.class);
        String str = "";
        if(task != null){
        	str = task.value();
        }
        logger.info(str + "定时器开始执行");
        
        
        long t = System.currentTimeMillis();
        try {
			result = joinPoint.proceed();
		} catch (RemoteAccessException e) {
			logger.error("未找到服务:{}.{}",clazz.getName(),targetMethod.getName());
		}
        logger.info(str + "定时器执行结束,耗时" + String.valueOf(System.currentTimeMillis() - t));
        return result;
    }
    
    
}
